package hao.bk.parking;

import hao.bk.royalmap.R;
import hao.bk.royalmap.RoyalMapActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class ParkingSaveLayout extends LinearLayout implements OnClickListener{
	private RoyalMapActivity main;
	//private TextView txtHello;
	protected ParkingGeneralLayout generalLayout;
	private Spinner spin1,spin2,spin3,spin4;
	private TextView infoSpint1, infoSpint2, infoSpint3, infoSpint4, timeSaveInfo;
	private LinearLayout timeSaveInfoLnl;
	Button btnSaveInfo;
	private boolean flagButton = true;
	
	/**
	 * Dữ liệu cho spinner
	 * @param context
	 */
	 String arr1[] = { "B3", "B4", "B5" };
	 String arr2[] = { "R1","R2","R3","R4","R5","R6"};
	 String arr3[] = { "A","B","C","D","E","F","G","H","I","J","K","L",
			 "M","N","O","P","R","S","T","U","V","W","X","Y","Z"};
	 String arr4[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20" };
			 

	public ParkingSaveLayout(Context context) {
		super(context);
		initParams();
	}
	/**
	 * Khởi tạo các tham số
	 */
	private void initParams() {
		main = RoyalMapActivity.getInstance();
		this.generalLayout = main.parking.generalLayout;
		inflate(main, R.layout.parking_save_local_layout,this);
		
		spin1 = (Spinner)findViewById(R.id.spinner1);
		 //Gán Data source (arr) vào Adapter
		 ArrayAdapter<String> adapter1=new ArrayAdapter<String>(main,R.layout.spinner_center_item, arr1);
		 //phải gọi lệnh này để hiển thị danh sách cho Spinner
		 adapter1.setDropDownViewResource
		 (android.R.layout.simple_spinner_dropdown_item);
		 //Thiết lập adapter cho Spinner
		 spin1.setAdapter(adapter1);
		 infoSpint1 = (TextView)findViewById(R.id.info_spinner1);
		 spin2 = (Spinner)findViewById(R.id.spinner2);
		 //Gán Data source (arr) vào Adapter
		 ArrayAdapter<String> adapter2=new ArrayAdapter<String>(main,R.layout.spinner_center_item, arr2);
		 //phải gọi lệnh này để hiển thị danh sách cho Spinner
		 adapter2.setDropDownViewResource
		 (android.R.layout.simple_spinner_dropdown_item);
		 //Thiết lập adapter cho Spinner
		 spin2.setAdapter(adapter2);
		 infoSpint2 = (TextView)findViewById(R.id.info_spinner2);
		 spin3 = (Spinner)findViewById(R.id.spinner3);
		 //Gán Data source (arr) vào Adapter
		 ArrayAdapter<String> adapter3=new ArrayAdapter<String>(main,R.layout.spinner_center_item, arr3);
		 //phải gọi lệnh này để hiển thị danh sách cho Spinner
		 adapter3.setDropDownViewResource
		 (android.R.layout.simple_spinner_dropdown_item);
		 //Thiết lập adapter cho Spinner
		 spin3.setAdapter(adapter3);
		 infoSpint3 = (TextView)findViewById(R.id.info_spinner3);
		 spin4 = (Spinner)findViewById(R.id.spinner4);
		//Gán Data source (arr) vào Adapter
		 ArrayAdapter<String> adapter4=new ArrayAdapter<String>(main,R.layout.spinner_center_item, arr2);
		 //phải gọi lệnh này để hiển thị danh sách cho Spinner
		 adapter4.setDropDownViewResource
		 (android.R.layout.simple_spinner_dropdown_item);
		 //Thiết lập adapter cho Spinner
		spin4.setAdapter(adapter4);
		infoSpint4 = (TextView)findViewById(R.id.info_spinner4);
		timeSaveInfoLnl = (LinearLayout)findViewById(R.id.time_save_info_lnl);
		timeSaveInfo = (TextView)findViewById(R.id.time_save_info);
		btnSaveInfo = (Button)findViewById(R.id.save_parking_btn);
		btnSaveInfo.setOnClickListener(this);
		disPlayInfo();
	}
	public void disPlayInfo(){
		
		
		String sdcard=Environment
				 .getExternalStorageDirectory()
				 .getAbsolutePath()+"/royalsaveinfo.txt";
		try {
			 Scanner scan=new Scanner(new File(sdcard));
			 String line="";
			 while(scan.hasNext())
			 {
				 line = scan.nextLine();
				 String[] strings = TextUtils.split(line, ";");
				 	if(strings.length > 4) {
				 		flagButton = false;
				 		spin1.setVisibility(View.GONE);
						spin2.setVisibility(View.GONE);
						spin3.setVisibility(View.GONE);
						spin4.setVisibility(View.GONE);
						infoSpint1.setVisibility(View.VISIBLE);
						infoSpint2.setVisibility(View.VISIBLE);
						infoSpint3.setVisibility(View.VISIBLE);
						infoSpint4.setVisibility(View.VISIBLE);
						timeSaveInfoLnl.setVisibility(View.VISIBLE);
						timeSaveInfo.setVisibility(View.VISIBLE);
					    infoSpint1.setText(strings[0].trim());
					    infoSpint2.setText(strings[1].trim());
					    infoSpint3.setText(strings[2].trim());
					    infoSpint4.setText(strings[3].trim());
					    timeSaveInfo.setText(""+strings[4].trim());
					    btnSaveInfo.setText("Sửa");
					    btnSaveInfo.setTextColor(Color.WHITE);
				 	}
				 break;
			 }
			 scan.close();
			
			 } catch (FileNotFoundException e) {
			 e.printStackTrace();
			 }
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == btnSaveInfo){
			if(flagButton == true) {
				btnSaveInfo.setText("Sửa");
				btnSaveInfo.setTextColor(Color.WHITE);
				String text = "";
				text += spin1.getSelectedItem().toString() + ";" + spin2.getSelectedItem().toString() +
						 ";" + spin3.getSelectedItem().toString() +  ";" + spin4.getSelectedItem().toString();
				//Lấy về thời gian hệ thống, để lưu lại thời gian
				Calendar calendar =  GregorianCalendar.getInstance();
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH) + 1;
				int day = calendar.get(Calendar.DATE);
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				int min = calendar.get(Calendar.MINUTE);
				String time = hour + "h " +min + "ph" + " ngày " +day + "/" + month + "/" + year;
				text += ";" + time;
				
				// đường dẫn lưu file txt trong sdcard
				String sdcard=Environment
						 .getExternalStorageDirectory()
						 .getAbsolutePath()+"/royalsaveinfo.txt";
				 try {
				 OutputStreamWriter writer=
				 new OutputStreamWriter(
				 new FileOutputStream(sdcard));
				 writer.write(text);
				 writer.close();
				 } catch (FileNotFoundException e) {
				 e.printStackTrace();
				 } catch (IOException e) {
				 e.printStackTrace();
				 }
				 
				spin1.setVisibility(View.GONE);
				spin2.setVisibility(View.GONE);
				spin3.setVisibility(View.GONE);
				spin4.setVisibility(View.GONE);
				infoSpint1.setVisibility(View.VISIBLE);
				infoSpint2.setVisibility(View.VISIBLE);
				infoSpint3.setVisibility(View.VISIBLE);
				infoSpint4.setVisibility(View.VISIBLE);	
				timeSaveInfoLnl.setVisibility(View.VISIBLE);
				timeSaveInfo.setVisibility(View.VISIBLE);
				// Lấy thông tin đã lưu hiện lên textView
				 try {
				 Scanner scan=new Scanner(new File(sdcard));
				 String line="";
				 while(scan.hasNext())
				 {
					 line = scan.nextLine();
					 String[] strings = TextUtils.split(line, ";");
						infoSpint1.setText(strings[0].trim());
						infoSpint2.setText(strings[1].trim());
						infoSpint3.setText(strings[2].trim());
						infoSpint4.setText(strings[3].trim());
						timeSaveInfo.setText("" + strings[4].trim());
						break;
				 }
				 scan.close();
				
				 } catch (FileNotFoundException e) {
				 e.printStackTrace();
				 }
				
				btnSaveInfo.setTextColor(Color.WHITE);
				flagButton = false ;
			}else {
				btnSaveInfo.setText("Ghi nhớ");
				btnSaveInfo.setTextColor(main.getResources().
						getColor(R.color.yellow_icon));
				flagButton = true;
				spin1.setVisibility(View.VISIBLE);
				spin2.setVisibility(View.VISIBLE);
				spin3.setVisibility(View.VISIBLE);
				spin4.setVisibility(View.VISIBLE);
				infoSpint1.setVisibility(View.GONE);
				infoSpint2.setVisibility(View.GONE);
				infoSpint3.setVisibility(View.GONE);
				infoSpint4.setVisibility(View.GONE);
				timeSaveInfoLnl.setVisibility(View.GONE);
				timeSaveInfo.setVisibility(View.GONE);
				spin1.setClickable(true);
				spin1.setBackgroundColor(main.getResources().getColor(R.color.visiable_spinner));
				spin2.setClickable(true);
				spin2.setBackgroundColor(main.getResources().getColor(R.color.visiable_spinner));
				spin3.setClickable(true);
				spin3.setBackgroundColor(main.getResources().getColor(R.color.visiable_spinner));
				spin4.setClickable(true);
				spin4.setBackgroundColor(main.getResources().getColor(R.color.visiable_spinner));
			}
		}
	}
}
